package com.tklab.smartcity.ui.home;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.android.material.tabs.TabLayout;
import com.tklab.smartcity.R;
import com.tklab.smartcity.adapter.NewsAdapter;
import com.tklab.smartcity.adapter.ServiceAdapter;
import com.tklab.smartcity.bean.NewsBean;
import com.tklab.smartcity.bean.NewsCatagroy;
import com.tklab.smartcity.bean.Rotation;
import com.tklab.smartcity.bean.ServiceBean;
import com.tklab.smartcity.bean.WertherBean;
import com.tklab.smartcity.ui.BaseFragment;
import com.tklab.smartcity.util.RetrofitUtil;

import java.util.List;

public class HomeFragment extends BaseFragment {

    private HomeViewModel model;
    private ViewFlipper vf;
    private TextView txtWeather;
    private RecyclerView recservice;
    private TabLayout tabNews;
    private RecyclerView recNews;

    @Override
    protected int layoutId() {
        return R.layout.fragment_home;
    }
    @Override
    protected void initView(View view) {
        vf=view.findViewById(R.id.vf);
        txtWeather=view.findViewById(R.id.txtWeather);
        recservice=view.findViewById(R.id.recService);
        recservice.setLayoutManager(new GridLayoutManager(getContext(),6));
        recNews=view.findViewById(R.id.recNews);
        recNews.setLayoutManager(new GridLayoutManager(getContext(),1));


        //设置布局
        tabNews=view.findViewById(R.id.tabNews);
        tabNews.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                model.loadNews(tab.getTag().toString());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
    @Override
    protected void initData(Context context) {
        model=new ViewModelProvider(getActivity(),new ViewModelProvider.NewInstanceFactory())
        .get(HomeViewModel.class);
        initRotation();
        initWeather();
        initService();
        initNewsCatagroy();
        initNews();
    }

    private void initNews() {
        model.getNews().observe(getViewLifecycleOwner(), new Observer<NewsBean>() {
            @Override
            public void onChanged(NewsBean newsBean) {
                List<NewsBean.RowsBean> list=newsBean.getRows();
                recNews.setAdapter(new NewsAdapter(list));
            }
        });
    }

    private void initNewsCatagroy() {
        model.getNewsCatagroy().observe(getViewLifecycleOwner(), new Observer<NewsCatagroy>() {
            @Override
            public void onChanged(NewsCatagroy newsCatagroy) {
                for(NewsCatagroy.DataBean d:newsCatagroy.getData()){
                    tabNews.addTab(tabNews.newTab().setText(d.getName()).setTag(d.getId()));
                }
            }
        });
    }

    private void initService() {
        model.getService().observe(getViewLifecycleOwner(), new Observer<ServiceBean>() {
            @Override
            public void onChanged(ServiceBean serviceBean) {
//                获得数据
                List<ServiceBean.RowsBean> list=serviceBean.getRows();
//                绑定adapter
                recservice.setAdapter(new ServiceAdapter(list));
            }
        });
    }

    private void initWeather() {
        model.getWerther().observe(getViewLifecycleOwner(), new Observer<WertherBean>() {
            @Override
            public void onChanged(WertherBean wertherBean) {
                WertherBean.DataBean d=wertherBean.getData();
                txtWeather.setText("今天天气："+d.getTemperature()
                        +"°C  最高温度："+d.getMaxTemperature()+"°C");

            }
        });
    }

    private void initRotation() {
        model.getRotation().observe(getViewLifecycleOwner(), new Observer<Rotation>() {
            @Override
            public void onChanged(Rotation rotation) {
                List<Rotation.RowsBean> list=rotation.getRows();
                for (Rotation.RowsBean r:list){
                    String url=r.getAdvImg();
                    ImageView image=new ImageView(getContext());
                    image.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    Glide.with(getContext()).load(RetrofitUtil.BASE_URL+url).into(image);
                    vf.addView(image);
                }
            }
        });
    }
}